package com.caiya.controller;

import com.caiya.cart.Res;
import com.caiya.cart.dto.user.CartUserInfoDto;
import com.caiya.cart.entity.CartUser;
import com.caiya.cart.vo.user.CartUserAddVo;
import com.caiya.cart.vo.user.CartUserInfoVo;
import com.caiya.cart.vo.user.CartUserListVo;
import com.caiya.cart.vo.user.CartUserUpdateVo;
import com.caiya.service.UserCartService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by Administrator on 2018/11/28 0028.
 */
@RestController
@Api(description = "用户")
@RequestMapping("/api/user")
public class UserCartController {

    @Autowired
    private UserCartService userCartService;

    /**
     *  此接口的用途
     * @return
     */
    @GetMapping(value = "/byid/{id}")
    @ApiOperation(value = "iii",notes = "iii")
    public Res byId(@PathVariable("id")Long id){
        Res res = null;
        try {
            CartUser cartUser = userCartService.buId(id);
            res = new Res(cartUser);
        }catch (Exception e){
            e.printStackTrace();
            res = new Res(Res.CODE_FAIL,e.getMessage());        }
        return res;
    }

    @PostMapping(value = "/list")
    @ApiOperation(value = "用户列表",notes = "列表")
    public Res list(@RequestBody CartUserListVo cartUserListVo){
        Res res = null;
        try {
            PageInfo<CartUser> info = userCartService.list(cartUserListVo);
            res = new Res(info);
        }catch (Exception e){
            e.printStackTrace();
            res = new Res(Res.CODE_FAIL,e.getMessage());
        }
        return res;
    }

    @PostMapping(value = "/add")
    @ApiOperation(value = "新增",notes = "新增")
    public Res add(@RequestBody CartUserAddVo cartUserAddVo){
        Res res = null;
        try {
            int i = userCartService.add(cartUserAddVo);
            res = new Res(i);
        }catch (Exception e){
            e.printStackTrace();
            res = new Res(Res.CODE_FAIL,e.getMessage());
        }
        return res;
    }

    @PostMapping(value = "/update")
    @ApiOperation(value = "更新",notes = "更新")
    public Res update(@RequestBody CartUserUpdateVo cartUserUpdateVo){
        Res res = null;
        try {
            int i = userCartService.update(cartUserUpdateVo);
            res = new Res(i);
        }catch (Exception e){
            e.printStackTrace();
            res = new Res(Res.CODE_FAIL,e.getMessage());
        }
        return res;
    }

    @PostMapping(value = "/info")
    @ApiOperation(value = "info",notes = "info")
    public Res info(@RequestBody CartUserInfoVo cartUserInfoVo){
        Res res = null;
        try {
            CartUserInfoDto cartUserInfoDto = userCartService.info(cartUserInfoVo);
            res = new Res(cartUserInfoDto);
        }catch (Exception e){
            e.printStackTrace();
            res = new Res(Res.CODE_FAIL,e.getMessage());
        }
        return res;
    }
}
